#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e7 + 5;

int n, m;
int np[N], phi[N];
vector<int> primes;
void init(int n) {
  np[1] = phi[1] = 1;
  rep(i, 2, n) {
    if (!np[i]) {
      primes.push_back(i);
      phi[i] = i - 1;
    }
    for (int j : primes) {
      ll k = 1ll * j * i;
      if (k > n) break;
      np[k] = 1;
      if (i % j == 0) {
        phi[k] = phi[i] * j;
        break;
      } else
        phi[k] = phi[i] * (j - 1);
    }
  }
}
ll fastpow(ll b, ll p, ll mod) {
  ll res = 1;
  while (p) {
    if (p & 1) res = res * b % mod;
    b = b * b % mod;
    p >>= 1;
  }
  return res;
}
int f(int p) {  // M % p = 2^((M % phi(p)) + phi(p)) (mod p)
  if (p == 1) return 0;
  return fastpow(2, f(phi[p]) + phi[p], p);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  init(1e7);
  int t;
  cin >> t;
  while (t--) {
    int p;
    cin >> p;
    if (p == 1) {
      cout << 0 << endl;
      continue;
    }
    ll ans = f(p);
    cout << ans << endl;
  }

  return 0;
}